//
//  WelcomePageViewController.swift
//  CSYMicroBlockSina
//
//  Created by 姚彦兆 on 15/11/12.
//  Copyright © 2015年 姚彦兆. All rights reserved.
//

import UIKit
import SnapKit

import SDWebImage

class WelcomePageViewController: UIViewController {
    
    override func loadView() {
        
        view = welcomeImageView
        
    }
    
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        
        startAnimation()
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setUpUI()
        
        // Do any additional setup after loading the view.
    }
    
    private func startAnimation(){
    
        //先修改约束
        iconImageView.snp_updateConstraints { (make) -> Void in
            
            make.bottom.equalTo(self.view.snp_bottom).offset(-screen_Height + screen_Height * 0.3)
            
//            make.bottom.equalTo(self.view.snp_bottom).offset(-self.view.bounds.height + 180)
            
        }
        
        UIView.animateWithDuration(1.25, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.75, animations: { () -> Void in
                    
                    self.nameLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        
                        NSThread.sleepForTimeInterval(1)
                        
                        NSNotificationCenter.defaultCenter().postNotificationName(APPDELEGATESHOULDSWITCHROOTVIEWCONTROLLER, object: nil)
                        
                })
                
        }
        
        
    }
    
    private func setUpUI(){
    
        view.addSubview(iconImageView)
        
        view.addSubview(nameLabel)
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view.snp_centerX)
            
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            
            make.width.equalTo(90)
            
            make.height.equalTo(90)
            
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(iconImageView.snp_bottom).offset(10)
            
            make.centerX.equalTo(view.snp_centerX).offset(0)
            
            
            
        }
        
        iconImageView.layer.cornerRadius = 45
        
        iconImageView.clipsToBounds = true
        
        iconImageView.layer.masksToBounds = true
    
        iconImageView.sd_setImageWithURL(GetUserInfoModel().userIcon, placeholderImage: UIImage(named: "avatar_default_big"))
        
    }

    //懒加载所有的控件
    lazy var welcomeImageView : UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    lazy var iconImageView : UIImageView = UIImageView()
    
    lazy var nameLabel : UILabel = {
    
        let label = UILabel()
        
        label.text = "瓦塔西瓦\n" + (GetUserInfoModel().userName ?? "")
        
        label.textAlignment = .Center
        
        label.font = UIFont.systemFontOfSize(14)
        
        label.textColor = UIColor.lightGrayColor()
        
        label.numberOfLines = 0
        
        label.alpha = 0
        
        label.sizeToFit()
        
        return label
    
    }()
    

}
